package com.sk.bilibili.other;

import com.sk.bilibili.service.*;
import com.sk.bilibili.utils.DbUtils;
import com.sk.bilibili.utils.RedisUtils;
import com.sk.bilibili.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 邵康
 * @date 2021/12/4 18:34
 */
@Component
public class ScheduleTask {

    @Autowired
    private DbUtils dbUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SearchService searchService;
    @Autowired
    private ThemBannerService themBannerService;
    @Autowired
    private BannerService bannerService;

    /**
     * 每天午夜0点清除奖励信息
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void clearDailyReward(){
        redisUtils.clearDailyReward();
    }

    /**
     * 每天午夜0点30清除7天前的硬币变化记录
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void clearCoinRecord(){
        dbUtils.clearRecord("coin_record",postTime(7));
    }

    /**
     * 每天午夜1点清除7天前的投币记录
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void clearCoin(){
        dbUtils.clearRecord("coin",postTime(7));
    }

    /**
     * 每天午夜1点30清除7天前的历史记录
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void clearHistory(){
        dbUtils.clearRecord("history",postTime(7));
    }

    /**
     * 每天午夜2点只保留每个人前 20条搜索记录
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void clearSearch(){
        searchService.clearSearch();
    }

    /**
     * 每天午夜2点30把redis的数量信息转移到Mysql并清空Redis数量信息
     */
    @Scheduled(cron = "0 30 2 * * ?")
    public void flashNum(){
        redisUtils.flashNum();
    }

    /**
     * 每分钟 同步Redis数据库所有点赞信息
     */
    @Scheduled(cron = "0 * * * * ?")
    public void flashPraise(){
        redisUtils.flashPraise();
    }

    /**
     * 每周1 0点更新 轮播图地址
     */
    @Scheduled(cron="0 0 0 ? * MON")
    public void updateBanner(){
        themBannerService.updateThemBanner();
        bannerService.updateBanner();
    }

    private long postTime(int day) {
        return System.currentTimeMillis()-day* TimeUtils.DAY;
    }


}
